package com.hz.controller;

import com.hz.pojo.SplitRecord;
import com.hz.service.SplitRecordService;
import com.hz.utils.MassageJson;
import com.hz.utils.MassageJsons;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.List;

@RestController
@RequestMapping("/SplitRecord")
public class SplitRecordController {

    @Resource
    private SplitRecordService splitRecordService;

    @PostMapping("/addSplitRecord")
    public MassageJson addSplitRecord(String order_code){
        int i = 0;
        try {
            i = splitRecordService.addSplitRecord(order_code);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new MassageJson(200,"新增成功",i);
    }

    /**
     * 查询用户分成明细，按照推广方式查询
     * @param split_user_id     用户id
     * @param split_method      分成方式
     * @return
     */
    @RequestMapping("/getUserFenCheng")
    public MassageJsons getUserFenCheng(Long split_user_id,Integer split_method){
        List<SplitRecord> list = null;
        try {
            list = splitRecordService.getUserFenCheng(split_user_id,split_method);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new MassageJsons("0","请求成功",list);
    }

    /**
     * 计算用户推广明细
     * @param split_user_id 用户id
     * @return
     */
    @RequestMapping("/getUserFenChengCount")
    public MassageJsons getUserFenChengCount(Long split_user_id){
        List<SplitRecord> list = null;
        DecimalFormat df   = new DecimalFormat("######0.00");
        Double count = 0.00;
        try {
            list = splitRecordService.getUserFenChengCount(split_user_id);
            for (SplitRecord splitRecord:list) {
                count+=splitRecord.getSplit_money();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new MassageJsons("0","请求成功",df.format(count));
    }
}
